package edu.suda.xsap.service.impl;

import com.github.pagehelper.PageHelper;
import edu.suda.xsap.dto.EmployeeDto;
import edu.suda.xsap.dto.convert.EmployeeConvert;
import edu.suda.xsap.entity.Employee;
import edu.suda.xsap.mapper.EmployeeMapper;
import edu.suda.xsap.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeConvert employeeConvert;

    @Override
    public List<EmployeeDto> findAll(int page, int size) {
        PageHelper.startPage(page, size);
        List<Employee> employees = employeeMapper.findAll();
        return employeeConvert.toDtoList(employees);
    }

    @Override
    public EmployeeDto save(EmployeeDto employeeDto) {
        Employee employee = employeeConvert.toEntity(employeeDto);
        employeeMapper.save(employee);
        return employeeConvert.toDto(employee);
    }

    @Override
    public EmployeeDto update(EmployeeDto employeeDto) {
        if (employeeDto.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        Employee employee = employeeConvert.toEntity(employeeDto);
        employeeMapper.update(employee);
        return employeeConvert.toDto(employee);
    }

    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }
}